package derp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.*;

//Make results work
//GUIfy this shit (GUI design first)
//Note that underscore is a forbidden character (import/export) possibly find a way to prevent its use
public class Client {
	//lists of all Choices and Options, used for import/export and playing
	private static ArrayList<Choice> choices = new ArrayList<Choice>();
	private static ArrayList<Option> allOptions = new ArrayList<Option>();
	private static ArrayList<Statistic> statistics = new ArrayList<Statistic>();
	//used for export caesar-ing, not essential (still dont know exactly how it works)
	private static int magicNumber = 50;
	public static void main(String[] args) throws FileNotFoundException{
		Scanner console = new Scanner(System.in);
		
		String[] testResults = new String[]{"This", "is", "a", "test"};
		Option option1 = new Option("This will be displayed at first", "This will be displayed when I am clicked", testResults, "choice1");
		Option option2 = new Option("Also a test", "You have clicked me", testResults, "choice2");
		ArrayList<Option> options = new ArrayList<Option>();
		options.add(option1);
		Option BrantisHot = new Option("Eheu!", "Bosoms will be bestowed upon ye", testResults, "choice1");
		options.add(option2);
		options.add(BrantisHot);
		Choice choice1 = new Choice(options, "You should click some shit", "choice1");
		Choice choice2 = new Choice(options, "I am another choice brah", "choice2");
		choices.add(choice1);
		choices.add(choice2);
		Statistic res = new Statistic("Res", true, "bool");
		Statistic res2 = new Statistic("Res", 5, "int");
		Statistic res3 = new Statistic("Res", "RAWR", "str");
		
		exporter(console);
		choices.clear();
		allOptions.clear();
		importer(console);
//		System.out.println(choices.toString());
//		System.out.println(allOptions.toString());
//		String derp = caesar("C_[This will be displayed at first, Also a test, Eheu!]_I am another choice brah_choice2", 2);
//		System.out.println(antiCaesar(derp));
		dispChoice(0, console);
	}
	
	public static int getIoLink(String link){ //gets the index of a link (used for manipulating Choices)
		for (int i = 0; i < choices.size(); i++){
			if (choices.get(i).getLink().equals(link))
				return i;
		}
		return -1; //returns -1 if link is nonexistent (spelling of link MUST be correct)
	}
	
	public static void removeChoice(String link){ //removes a Choice based off of link
		choices.remove(getIoLink(link));
	}
	
	public static Choice followLink(Option option){ //finds a Choice based off of link (used for linking Options and Choices)
		String link = option.getLink();
		Choice choice = choices.get(getIoLink(link));
		return choice;
	}
	
	public static Option getMeAMotherfuckingOption(String dispText){ //returns an option when given that options display text
		for (int i = 0; i < allOptions.size(); i++){
			if (allOptions.get(i).getDispText().equals(dispText))
				return allOptions.get(i);
		}
		return null;
	}
	
	public static ArrayList<String> getAllLinks(){ //finds all links included in choice
		ArrayList<String> links = new ArrayList<String>();
		for (int i = 0; i < choices.size(); i++){
			String link = choices.get(i).getLink();
			links.add(i, link);
		}
		return links;
	}	
	
	public static boolean uniqueLink(String link){ //checks to see if a link is unique
		boolean bool = true;
		for (int i = 0; i < choices.size(); i++){
			if (link.equals(choices.get(i).getLink())){
				bool = false;
			}
		}
		return bool;
	}
	
	
	//for testing - prints out choices at this link and follows them; more or less works for playing the game
	public static void dispChoice(int index, Scanner in){ 
		Choice choice = choices.get(index);
		System.out.println(choice);
		for (int i = 0; i < choice.getOptions().size(); i++){
			System.out.println("Option " + (i + 1) +": " + choice.getOptions().get(i));
		}
		int picked = in.nextInt() - 1;
		Option pickedOp = choice.getOptions().get(picked);
		System.out.println(pickedOp.getResText());
		System.out.println("----");
		dispChoice(getIoLink(pickedOp.getLink()), in);
	}
	
	
	public static void merge(){ //merges the options into one list
		int index = 0;
		for (int i = 0; i < choices.size(); i++){
			ArrayList<Option> options = choices.get(i).getOptions();
			for (int j = 0; j < options.size(); j++){
				boolean works = true;
				for (int k = 0; k < allOptions.size(); k++){
					if (allOptions.get(k).equals(options.get(j)))
						works = false;
				}
				if (works)
					allOptions.add(options.get(j));
			}
		}
	}
	
	//note: this requires allOptions and choices to be filled
	public static void exporter(Scanner in) throws FileNotFoundException{ //exports to a text file
		merge();
		System.out.println("Please input file path to export to: ");
		String filepath = in.next();
		System.out.println("Please input file name: ");
		String filename = in.next();
		//File outputFile = new File(filepath + filename);
		//File outputFile = new File("C:/Users/Kian/Documents/Garfield/AP CS++/Java/HerpDerp/src/moartesting");
		File outputFile = new File("H:/APCS/Eclipse/HerpDerp/src/testing");
		PrintStream outFile = new PrintStream(outputFile);
		for (int j = 0; j < allOptions.size(); j++){
			int random = (int) (Math.random() * magicNumber);
			Option option = allOptions.get(j);
			String stringCheese = ("O_" + option.getDispText() + "_" + option.getResText() + "_" + Arrays.toString(option.getResults()) + "_" + option.getLink());
			outFile.println(caesar(stringCheese, random));
		}
		for (int i = 0; i < choices.size(); i++){
			int random = (int) (Math.random() * magicNumber);
			Choice choice = choices.get(i);
			String stringCheese = ("C_" + choice.getOptions() + "_" + choice.getDispText() + "_" + choice.getLink());
			outFile.println(caesar(stringCheese, random));
		}
	}
	
	//note: this fills both allOptions and choices (assumption is user is starting from scratch) so clear them for testing
	public static void importer(Scanner in) throws FileNotFoundException{ //imports from a text file
		System.out.println("Please input complete file path of input file: ");
		String filepath = in.next();
		//Scanner inputFile = new Scanner(filepath);
		//Scanner inputFile = new Scanner(new File("C:/Users/Kian/Documents/Garfield/AP CS++/Java/HerpDerp/src/moartesting"));
		Scanner inputFile = new Scanner(new File("H:/APCS/Eclipse/HerpDerp/src/testing"));
		while (inputFile.hasNextLine()){
			String line = antiCaesar(inputFile.nextLine());
			String key = line.substring(0, 1);
			if (key.equals("O")){
				line = line.substring(2);
				String dispText = line.substring(0, line.indexOf('_'));
				line = line.substring(line.indexOf('_'));
				String resText = line.substring(0, line.indexOf('_'));
				line = line.substring(line.indexOf('_'));
				ArrayList<String> results = new ArrayList<String>();
				while (line.contains(",")){
					String result = line.substring(0, line.indexOf(',') + 1);
					line = line.substring(line.indexOf(',') + 1);
					results.add(result);
				}
				line = line.substring(line.indexOf(']') + 2);
				String link = line;
				String[] resultsArray = new String[results.size()];
				for (int i = 0; i < results.size(); i++){
					resultsArray[i] = results.get(i);
				}
				Option option = new Option(dispText, resText, resultsArray, link);
				allOptions.add(option);
			}
			if (key.equals("C")){
				line = line.substring(2);
				ArrayList<Option> options = new ArrayList<Option>();
				String str = line.substring(1, line.indexOf('_') - 1);
				int counter = 0;
				while (str.contains(",")){
					String result = str.substring(0, str.indexOf(','));
					str = str.substring(str.indexOf(',') + 2);					
					Option option = getMeAMotherfuckingOption(result);
					options.add(counter, option);
					counter++;
				}
				Option option = getMeAMotherfuckingOption(str);
				options.add(counter, option);
				line = line.substring(line.indexOf('_') + 1);
				String dispText = line.substring(0, line.indexOf('_'));
				line = line.substring(line.indexOf('_') + 1);
				String link = line;
				Choice choice = new Choice(options, dispText, link);
				choice.setOptions(options);
				choices.add(choice);
			}
			else if (!key.equals("O") && !key.equals("C")){
				throw new IllegalArgumentException("Shit shit shit shit shit that shouldn't happen shit shit shit shit");
			}
		}
	}
	
	
	//This is just me messing around - makes text files look really cool through a randomly assigned caesar cipher
	//Doesn't appear to mess anything up with import/export, so just going to keep it in
	public static String caesar(String str, int mod){ //Caesar ciphers each character in thing string by the modifer
		String newStr = "";
		for (int i = 0; i < str.length(); i++){
			char care = str.charAt(i);
			for (int j = 0; j <= mod; j++){
				if (care == 126)
					care = 32;
				else
					care++;
			}
			newStr += care;
		}
		return newStr;
	}
	
	//Future plans: allow user to set the key when exporting, just in case they like underscores
	public static String antiCaesar(String caesard){
		char key = caesard.charAt(1);
		int cheese = 0;
		for (int i = 1; i < caesard.length(); i++){ //finds out how many underscores (well, caesard ones) there are
			if (caesard.charAt(i) == key) 			//this is used to determine whether it is an Option or a Choice
				cheese++;							
		}
		int mod = 0;
		if (cheese == 3)
			mod = caesard.charAt(0) - 'C';		
		if (cheese == 4)
			mod = caesard.charAt(0) - 'O';
		String string = "";
		for (int i = 0; i < caesard.length(); i++){
			char care = caesard.charAt(i);
			for (int j = 0; j < mod; j++){
				if (care == 32)
					care = 126;
				else
					care--;
			}
			string += care;
		}
		return string;
	}
}
